package com.yimiyisu.contract_cust.controller;

import com.zen.ZenController;
import com.zen.ZenData;
import com.zen.ZenEngine;
import com.zen.ZenResult;
import com.zen.annotation.AccessRole;
import com.zen.annotation.Inject;
import com.zen.enums.ZenRole;
import com.zen.kit.IOKit;

@AccessRole(ZenRole.ADMIN)
public class Funds extends ZenController {
    @Inject
    private ZenEngine zenEngine;

    /**
     * 添加账款
     */
    public ZenResult addFund(ZenData data) {
        long amount = Long.parseLong(data.get("amount"));
        String contractId = data.get("related");
        ZenData contractData = ZenData.create("id",contractId);
        ZenResult contractResult = zenEngine.execute("get/contract", contractData);
        if("2".equals(data.get("type"))){
            long receivable = contractResult.getLong("receivable");
            if(receivable < amount){
                return ZenResult.fail("回款金额大于应收金额");
            }
            contractData.put("receivable", receivable - amount);
            contractData.put("receipts",contractResult.getLong("receipts") + amount);
            zenEngine.execute("patch/contract", contractData);
        }else{
            long payable = contractResult.getLong("payable");
            if(payable < amount){
                return ZenResult.fail("付款金额大于应付金额");
            }
            contractData.put("payable", payable - amount);
            contractData.put("payment",contractResult.getLong("payment") + amount);
            contractData.put("verification",contractResult.getLong("verification") + amount);
            data.put("status",1);
            zenEngine.execute("patch/contract", contractData);
        }
        zenEngine.execute("put/funds", data);
        return ZenResult.success();
    }

    /**
     * 核销账款
     */
    public ZenResult verification(ZenData data) {
        long amount = Long.parseLong(data.get("amount"));
        String contractId = data.get("related");
        ZenData contractData = ZenData.create("id",contractId);
        ZenResult contractResult = zenEngine.execute("get/contract", contractData);
        contractData.put("verification",contractResult.getLong("verification") + amount);
        zenEngine.execute("patch/contract", contractData);
        zenEngine.execute("patch/funds_verification", data);
        return ZenResult.success();
    }
}
